﻿using System;
using System.Windows.Controls;
using CI.Services;
using Microsoft.Practices.ServiceLocation;

namespace CI.Views.Diagramming
{
    /// <summary>
    /// Interaction logic for DiagramView.xaml
    /// </summary>
    public partial class DiagramView : UserControl
    {
        private readonly IDiagrammingServiceViewSide _diagrammingService = ServiceLocator.Current.GetInstance<IDiagrammingServiceViewSide>(); 

        public DiagramView()
        {
            _diagrammingService.Relayout += new EventHandler<GraphLayoutEventArgs>(DiagrammingServiceRelayout);

            InitializeComponent();

            //var settings = new GraphLayoutAlgorithmSettings() { AlgorithmTypeName = "EfficientSugiyama" };
            //ForceNewLayout(settings);
        }

        void DiagrammingServiceRelayout(object sender, GraphLayoutEventArgs e)
        {
            ForceNewLayout(e.Settings);
        }

        private void ForceNewLayout(GraphLayoutAlgorithmSettings parameters)
        {
            try
            {
                GraphLayout.LayoutAlgorithmType = parameters.AlgorithmTypeName;
                GraphLayout.Relayout();
            }
            catch (Exception)
            {
                //Thread.Sleep(50);
                //ForceNewLayout(parameters);
            }
            
        }
    }

}
